#!/bin/sh
# PCP QA Test No. 1147
# mutual exclusion for pmlogger init script?
# this version is using systemctl (see 1146 for dual)
#
# Copyright (c) 2019 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

[ "$PCPQA_SYSTEMD" = yes ] || _notrun "systemctl not installed or not active"

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
    _service pmlogger start | _filter_pcp_start
    _wait_for_pmlogger
    _restore_auto_restart pmlogger
}

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

# make sure systemctl thinks pmlogger is running
#
_service -v pmlogger restart >>$seq_full 2>&1
_wait_for_pmlogger || _exit 1

pid=`_get_primary_logger_pid`
echo "+++ at the start primary pmlogger pid = $pid" >>$seq_full
if [ -z "$pid" ]
then
    echo "Arrgh ... no primary pmlogger?"
    echo "Likely looking processes ..."
    # Note: no ( |$) for egrep, want to find pmlogger_* as well
    $PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep -E '[P]PID|/[p]mlogger'
    exit
fi
_stop_auto_restart pmlogger
if ! _service pmlogger stop; then _exit 1; fi \
| _filter_pcp_stop
_wait_pmlogger_end $pid || _exit 1

# real QA test starts here
for i in 1 2 3 4
do
    _service -v pmlogger start >$tmp.$i 2>&1 &
done

wait
_wait_for_pmlogger || _exit 1

for i in 1 2 3 4
do
    echo "== $i ==="
    _filter_pcp_start <$tmp.$i
done

./870 --check

echo "+++ at the end primary pmlogger pid = `_get_primary_logger_pid`" >>$seq_full

# success, all done
status=0
exit
